package com.generator.purchaseManage.productTypeManage.controller;

import com.generator.aboutUser.user.pojo.Users;
import com.generator.purchaseManage.brandManage.pojo.CBrand;
import com.generator.purchaseManage.brandManage.service.impl.BrandService;
import com.generator.purchaseManage.productTypeManage.pojo.CType;
import com.generator.purchaseManage.productTypeManage.service.impl.ProductManageService;
import com.generator.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @version 1.0
 * @auther wumei
 * @date 2022/10/05
 * 星期三 23:24
 */
@Controller
@RequestMapping("/productType")
public class ProductManageController {

    @Autowired
    private ProductManageService productManageService;

    public ProductManageService getProductManageService() {
        return productManageService;
    }

    public void setProductManageService(ProductManageService productManageService) {
        this.productManageService = productManageService;
    }

    @Autowired
    private BrandService brandService;

    public BrandService getBrandService() {
        return brandService;
    }
    public void setBrandService(BrandService brandService) {
        this.brandService = brandService;
    }


    /**
     * 获取所有商品类型分页
     * @param pageModel
     * @param type
     * @param model
     * @return
     */
    @RequestMapping("/getProductTypesByCondition")
    public String getProductTypesByCondition(PageModel<CType> pageModel, CType type, Model model){
        PageModel<CType> productTypeList = productManageService.getProductTypeByCondition(type, pageModel);
        model.addAttribute("productTypeList",productTypeList);
        return "purchase/productType/productTypeList";
    }

    /**
     * 查询所有商品类型信息
     * @return
     */
    @ResponseBody
    @RequestMapping("/getTypesList")
    public List<CType> getTypesList(){
        return productManageService.getProductTypeList(new CType());
    }


    /**
     * 添加商品类型
     * @param type
     * @return
     */
    @ResponseBody
    @RequestMapping("/addProductType")
    public boolean addProductType(CType type, HttpSession session) {
        Users user = (Users) session.getAttribute("user");
        type.setCreateId(user.getUId());
        // 通过品牌名称查询到品牌ID
        CBrand brand = new CBrand();
        brand.setBrandName(type.getBrandName());
        CBrand brand1 = brandService.queryBrandByName(brand);
        if (brand1 != null){
            type.setBrandId(brand1.getBrandId());
            return productManageService.addProductType(type);
        }
        return false;
    }

    /**
     * 跳转到修改商品类型页面
     * @param type
     * @param model
     * @return
     */
    @RequestMapping("/toUpdateProductTypeView/{typeName}/{brandName}")
    public String toUpdateProductTypeView(CType type,HttpSession session,Model model){
        // 根据商品类型名查询到商品类型信息
        CType updateProductType = productManageService.queryProductTypeByName(type);
        model.addAttribute("updateProductType",updateProductType);
        // 把当前修改的商品类型信息放入session供updateProductType方法使用
        session.setAttribute("updateProductType",updateProductType);
        return "purchase/productType/productTypeUpdate";
    }

    /**
     * 修改商品类型信息
     * @param type
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateProductType")
    public boolean updateProductType(CType type,HttpSession session){
        CType updateProductType = (CType) session.getAttribute("updateProductType");
        // 设置要修改的商品类型ID
        type.setTypeId(updateProductType.getTypeId());
        // 设置修改后的品牌ID
        CBrand brand = new CBrand();
        brand.setBrandName(updateProductType.getBrandName());
        CBrand brand1 = brandService.queryBrandByName(brand);
        type.setBrandId(brand1.getBrandId());
        if (type != null){

            return productManageService.updateProductType(type);
        }
        return false;
    }

    /**
     * 删除商品类型信息
     * @param type
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteProductType")
    public boolean deleteProductType(CType type){
        if (type != null){
            CType cType = productManageService.queryProductTypeByName(type);
            return productManageService.deleteProductType(cType);
        }
        return false;
    }



}
